<template>
  <div class="news">
    <banner title="新闻资讯" />
    <div class="news-section" v-loading="loading">
      <div class="news-section-content">
        <div class="content-nav">
          <div
            class="content-nav-btn"
            :class="{ 'content-nav-active': newsType === 1 }"
            @click="newsType = 1"
          >
            <span>公司新闻</span>
          </div>
          <div
            class="content-nav-btn"
            :class="{ 'content-nav-active': newsType === 2 }"
            @click="newsType = 2"
          >
            <span>行业动态</span>
          </div>
        </div>

        <TransitionGroup
          name="slide-up"
          appear
          tag="div"
          class="content-nav-item"
        >
          <div
            class="item-list"
            :key="item.id"
            v-for="item in newsListByType[newsType]"
          >
            <NuxtLink class="text-decoration" :to="`/NewsDetails/${item.id}`">
              <img :src="getFirstImageUrl(item.content)" alt />
              <p class="item-list-title">{{ item.title }}</p>
            </NuxtLink>
          </div>
        </TransitionGroup>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import Banner from "../components/Banner";

const loading = ref(true);
const newsListByType = ref({ 1: [], 2: [] });
const newsType = ref(1);

loading.value = true;

try {
  const { data: newsData } = await baseFetch("news/all");
  const sortedData = newsData.value.sort((a, b) => {
    return new Date(b.createTime) - new Date(a.createTime);
  });

  newsListByType.value = {
    1: sortedData.filter((e) => e.type === 1),
    2: sortedData.filter((e) => e.type === 2),
  };
  loading.value = false;
} catch (error) {
  console.error("数据加载失败:", error);
}
</script>

<style lang="scss" scoped>
@keyframes slideUpAnimation {
  0% {
    transform: translateY(50px);
    opacity: 0;
  }

  100% {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes imgZoom {
  0% {
    transform: scale(1);
  }

  100% {
    transform: scale(
      1.05
    ); 
  }
}

* {
  margin: 0;
  padding: 0;
}

.news {
  background: #f5f5f5;
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;

  &-section {
    width: 100%;
    background: #f5f5f5;

    &-content {
      margin: 0 auto;

      .content-nav {
        width: 40vw;
        height: 55px;
        margin: 0 auto;
        display: flex;
        align-items: center;
        position: relative;
        bottom: 30px;

        &-btn {
          width: 50%;
          height: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          background-color: #e4e4e4;
          transition: all 0.3s ease-out;

          &.content-nav-active {
            background-color: #001529;
            color: #fff;
          }
        }
      }

      .content-nav-item {
        justify-content: space-around;
        margin: 0 5vw;
        margin-bottom: 5%;
        display: flex;
        flex-wrap: wrap;

        .item-list {
          width: 70vw;
          display: flex;
          flex-direction: column;
          margin-bottom: 20px;

          &.slide-up-enter-active {
            animation: slideUpAnimation 1s ease-out;
          }

          &.slide-up-leave-active {
            animation: slideUpAnimation 1s ease-out reverse;
          }

          &.slide-up-enter,
          &.slide-up-leave-to {
            transform: translateY(50px);
            opacity: 0;
          }

          &:hover {
            animation: imgZoom 0.3s ease-out;
            animation-fill-mode: forwards;
          }

          &.slide-up-leave-to:not(:hover) {
            transform: translateY(0);
            opacity: 1;
            box-shadow: none;
            animation: none;
          }

          img {
            width: 70vw;
          }

          &-title {
            color: #000;
            font-size: 18px;
            margin: 1vw 0;
            white-space: normal;
            box-sizing: border-box; 
            max-width: 100%;
          }
        }

        @media screen and (max-width: 1000px) {
          .item-list {
            width: 88vw;

            img {
              width: 88vw;
            }

            &-title {
              font-size: 16px;
            }
          }
        }
      }
    }
  }

  .text-decoration {
    text-decoration: none;
  }
}
</style>
